package com.hapedu.gegeric;

import java.util.ArrayList;

public class Generic01 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("旺财",2));
        arrayList.add(new Dog("大黄",3));
        arrayList.add(new Dog("花花",1));
        arrayList.add(new Cat("招财猫",8));
        for (Object o : arrayList) {
            if (o instanceof Dog){
                Dog dog=(Dog) o;
                System.out.println(dog.getName()+dog.getAge()+"岁了");
            }
            else if (o instanceof Cat){
                Cat cat=(Cat) o;
                System.out.println(cat.getName()+cat.getAge()+"岁了");
            }

        }

        //假如添加一只猫

    }
}

class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat{
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}